package com.spit.fammo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import androidx.appcompat.widget.AppCompatTextView
import com.spit.fammo.R
import com.spit.fammo.bean.DataBean
import com.spit.fammo.ext.UPLOAD_PAGE


class MainDrawerExpandableAdapter(
    private val context: Context,
    private val listBean: List<DataBean>
) : BaseExpandableListAdapter() {

    override fun getGroupCount(): Int = listBean.size

    override fun getChildrenCount(groupPosition: Int): Int {
        return listBean[groupPosition].expandableChild.size
    }

    override fun getGroup(groupPosition: Int): Any {
        return listBean[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return listBean[groupPosition].expandableChild?.get(childPosition) ?: 0
    }

    override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

    override fun hasStableIds(): Boolean = false

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.i_expandable_group, parent, false)
        val bean = listBean[groupPosition]
        val tv_title: AppCompatTextView = view.findViewById(R.id.tv_title)
        val tv_num: AppCompatTextView = view.findViewById(R.id.tv_num)
        /*val iv_image: AppCompatImageView = view.findViewById(R.id.iv_image)
        val img: AppCompatImageView = view.findViewById(R.id.img)
        when(groupPosition){
            0 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_47)
            1 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_48)
            2 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_46)
            3 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_42)
            4 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_26)
            5 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_41)
        }
        img.visibility = if (groupPosition == 6)View.GONE else View.VISIBLE
        iv_image.visibility = if (bean.Type <= 2){View.VISIBLE}else{View.GONE}*/
        tv_title.text = bean.Title
        tv_num.visibility = if (bean.Type == UPLOAD_PAGE && bean.num != 0) {
            tv_num.text = if (bean.num < 100) {
                bean.num.toString()
            } else {
                "99+"
            }
            View.VISIBLE
        } else {
            View.GONE
        }
        /*view.setBackgroundColor(
            if (isExpanded) {
                context.getColor(R.color.colorPrimaryDark)
            } else {
                Color.TRANSPARENT // 未选中时的背景色
            }
        )*/
        return view
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val parentBean = listBean[groupPosition]
        val bean = parentBean.expandableChild.get(childPosition)
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.i_expandable_child, parent, false)
        val tv_title: AppCompatTextView = view.findViewById(R.id.tv_title)
        tv_title.text = bean?.Title

        // 设置 Child 的背景颜色
        /*view.setBackgroundColor(
            if (bean.isSelect) {
                context.getColor(R.color.colorPrimaryDark)
            } else {
                Color.TRANSPARENT // 未选中时的背景色
            }
        )*/

        return view
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

}